<script setup lang="js">
import {ref} from "vue"
import {goHome, goRegister} from "../../tools/go";
import {login} from "../../api/login/login";
import {getAuto, setAuto, setToken, setUserId} from "../../store";
import sha256 from "crypto-js/sha256";
import {bDialogue, bMessage} from "../../components/popup/popup.js";
import Dialogue from "../../components/popup/dialogue.vue";


const form_data = ref({
  userName:"",
  password:"",
  isAUTO: true,
})

const submit = () => {
  form_data.value.password = sha256(form_data.value.password) + ""
  login(form_data.value).then(res => {
    if(res.code === 200){
      setUserId(res.data.id)
      setToken(res.data.token)
      setAuto({
        username: form_data.value.userName,
        auth: res.data.auth
      })
      goHome()
    }else{
      form_data.value.userName = '';
      form_data.value.password = '';
      let msg = res.msg  === '' ? '系统出错' : res.msg;
      bDialogue("登录错误信息", msg);
    }
    form_data.value.password = '';
  })
}

const init = () => {
  let auto = getAuto();
  if (auto) {
    form_data.value.userName = auto.username
    form_data.value.password = auto.auth
    login(form_data.value).then(res => {
      // console.log(res)
      setUserId(res.data.id)
      setToken(res.data.token)
      setAuto({
        username: form_data.value.userName,
        auth: res.data.auth
      })
      goHome()
      form_data.value.password = '';
    })
  }
}

init()

</script>

<template>
  <view class="container">
    <view class="logo">
      <img src="../../static/img/logo.png" alt="">
      <text>智慧社区</text>
    </view>
    <view class="inpbox">
      <ul>
        <li>
          <label>用户名</label>
          <input  v-model="form_data.userName"  type="text" placeholder="请输入用户名">
        </li>
        <li>
          <label>密码</label>
          <input v-model="form_data.password" type="password" placeholder="请输入密码">
        </li>
      </ul>
    </view>
    <button class="submit" @click="submit">登录</button>
    <view class="register">
      <text @click="goRegister()">注册账号</text>
      <text >忘记密码</text>
    </view>
    <view class="tb">
      <view class="qq">
        <img src="../../static/img/qq.png" alt="">
        <img src="../../static/img/wechat.png" alt="">
        <img src="../../static/img/weibo.png" alt="">
      </view>
    </view>
  </view>
</template>

<style scoped lang="scss">
.container {
  display: flex;
  flex-direction: column;
  min-height: 100vh; // 确保容器至少占满整个视口高度
}

.tb {
  margin-top: auto; // 将 tb 推到底部
  .qq {
    display: flex;
    width:60%;
    justify-content: space-around;
    padding-bottom: 30px;
    img {
      width: 50px;
      border-right: 120px;
    }
  }
  display: flex;
  align-items: center;
  justify-content: center;
}

.register {
  text:nth-child(1) {
    border-right: 1px solid #000000;
  }
  text {
    width: 100px;
    height: 20px;
    color: #007aff;
    line-height: 20px;
    text-align: center;
  }
  width: 100%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.submit {
  margin: 25px auto;
  width: 80%;
  height: 40px;
  border-radius: 15px;
  text-align: center;
  line-height: 40px;
  color: #FFFFFF;
  font-size: 20px;
  background: #E07320;
}

.inpbox {
  ul {
    li {
      input {
        width: 77%;
        height: 50px;
        padding: 0 12px;
      }
      label {
        font-size: 20px;
        width: 60px;
      }
      height: 50px;
      margin-top: 5px;
      display: flex;
      align-items: center;
      padding: 0 12px;
      border-bottom: 3px solid #f4f4f4;
    }
    width: 80%;
    /*border-bottom: 3px solid #f4f4f4;*/
    display: flex;
    flex-direction: column;
  }
  width: 100%;
  height: 120px;
  padding-top: 10px;
  display: flex;
  justify-content: center;
}

.logo {
  text {
    font-size: 30px;
    color: #E07320;
  }
  img{
    width:100px;
  }
  width: 100%;
  height: 220px;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: flex-end;
}

* {
  margin: 0;
  padding: 0;
  display: flow-root;
}
</style>